package com.vma.shoeSystem.controller;

import com.jfinal.core.Controller;
import com.vma.order.service.OrderService;
import com.vma.shoeSystem.util.ReturnsUtil;
import com.vma.shoes.bizModel.model.OrderList;
import com.vma.shoes.bizModel.model.User;
import com.vma.user.service.UserService;
import org.apache.velocity.tools.generic.DateTool;

import java.util.List;

/**
 * Created by chenzui on 2017/4/11.
 */
public class UserController extends Controller {
    public void qryUserList(){
        try{
            Integer cPage = getParaToInt("cPage");
            Integer pSize = getParaToInt("pSize");
            String  status = getPara("status");
            String phone = getPara("phone");
            String start = getPara("start");
            String end = getPara("end");
            renderJson(ReturnsUtil.returnSuccess(UserService.qryUserList(cPage,pSize,status,phone,start,end)));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }

    }

    /**
     * 重置密码
     */
    public void resetPwd(){
        try{
            Integer userId = getParaToInt("userId");
            UserService.resetPwd(userId);
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    /**
     * 用户详情，包含订单信息等
     */
    public void userDetail(){
        try{
            Integer userId = getParaToInt("userId");
            User user = UserService.userDetail(userId);
            setAttr("user",user);
            setAttr("date", new DateTool());
            // TODO: 2017/4/25 把时间格式化加入全局拦截器
            List<OrderList> orders =  OrderService.qryOrderList("","",1,100,"","",userId).getList();
            setAttr("orders",orders);
            renderVelocity("/shop/user/user_detail.html");
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }

    public void updateStatus(){
        try{
            Integer userId = getParaToInt("id");
            UserService.updateStatus(userId);
            renderJson(ReturnsUtil.returnSuccess("成功"));
        }catch (Exception e){
            e.printStackTrace();
            renderJson(ReturnsUtil.returnException(e.getMessage(),e));
        }
    }
}
